<?php

namespace App\Http\Controllers;

use App\Lib\Api;
use App\Service\WalletcodeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WalletcodeController extends Controller
{
    /**
     * TradeController constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    //检测steam钱包充值码
    public function checkWalletcode(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'walletcode' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $walletcode = $request->input('walletcode');
        $botId = $request->input('botId', 0);

        list($code, $result) = WalletcodeService::getInstance()->checkWalletcode($walletcode, $botId);

        return ($code == Api::CODE_OK) ? Api::success($result) : Api::error($code);
    }

    //创建用户充值记录
    public function createUserWalletcode(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'walletcode' => 'required',
            'steamId' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $walletcode = $request->input('walletcode');
        $steamId = $request->input('steamId');
        $botId = $request->input('botId', 0);

        list($code, $result) = WalletcodeService::getInstance()->createUserWalletcode($walletcode, $steamId, $botId);

        return ($code == Api::CODE_OK) ? Api::success($result) : Api::error($code);
    }

    /**
     * 获取steam钱包充值状态
     */
    public function checkUserWalletcodeStatus(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'userWalletcodeId' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $userWalletcodeId = $request->input('userWalletcodeId');

        list($code, $result) = WalletcodeService::getInstance()->getUserWalletcodeInfo($userWalletcodeId);
        return ($code == Api::CODE_OK) ? Api::success($result) : Api::error($code);
    }

    /**
     * 获取机器人钱包金额
     */
    public function getBotBalance()
    {
        list($code, $result) = WalletcodeService::getInstance()->getBotBalance();
        return ($code == Api::CODE_OK) ? Api::success($result) : Api::error($code);
    }
}
